package com.xzmzhp.controller.people;

import com.github.pagehelper.PageHelper;
import com.xzmzhp.pojo.people.Society;
import com.xzmzhp.service.people.SocietyService;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author 王源
 * @Date: 2020/11/15 23:59
 * @Description: 社团信息前台交互类
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/society")
public class SocietyController {

    @Autowired
    private SocietyService societyService;

    /**
     * 查询所有社团信息
     *
     * @param page  起始
     * @param limit 查询数量
     * @return returnData
     */
    @PostMapping("/findAll")
    @ResponseBody
    public ReturnData findAll(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "limit", defaultValue = "10") int limit) {
        PageHelper.startPage(page, limit);
        return societyService.findAll();
    }

    /**
     * 根据社团名称查询社团信息
     *
     * @param societyName 社团名称
     * @param page        起始
     * @param limit       多少条
     * @return returnData
     */
    @PostMapping("/findBySocietyName")
    @ResponseBody
    public ReturnData findBySocietyName(@RequestParam(value = "page", defaultValue = "1") int page,
                                        @RequestParam(value = "limit", defaultValue = "10") int limit,
                                        @RequestParam("societyName") String societyName) {
        PageHelper.startPage(page, limit);
        return societyService.findBySocietyName(societyName);
    }

    /**
     * 根据学校名称查询社团信息
     *
     * @param societySchool 所属学校
     * @param page         起始
     * @param limit        多少条
     * @return returnData
     */
    @PostMapping("/societySchool")
    @ResponseBody
    public ReturnData findBySocialSchool(@RequestParam(value = "page", defaultValue = "1") int page,
                                         @RequestParam(value = "limit", defaultValue = "10") int limit,
                                         @RequestParam("societySchool") String societySchool) {
        PageHelper.startPage(page, limit);
        return societyService.findBySocialSchool(societySchool);
    }

    /**
     * 根据社团名称或所属学校查询社团信息
     *
     * @param societyName  社团名称
     * @param societySchool 所属学校
     * @param page         起始
     * @param limit        多少条
     * @return returnData
     */
    @RequestMapping("/findBySocial")
    @ResponseBody
    public ReturnData findBySocial(@RequestParam(value = "page", defaultValue = "1") int page,
                                   @RequestParam(value = "limit", defaultValue = "10") int limit,
                                   @RequestParam(value = "societySchool", defaultValue = "") String societySchool,
                                   @RequestParam(value = "societyName" , defaultValue = "") String societyName) {
        PageHelper.startPage(page, limit);
        return societyService.findBySocial(societyName, societySchool);
    }

    /**
     * 根据社团id查询社团信息
     *
     * @param societyId 社团id
     * @return returnData
     */
    @PostMapping("/findBySocialId")
    @ResponseBody
    public ReturnData findBySocialId(@RequestParam("societyId") Integer societyId) {
        return societyService.findBySocialId(societyId);
    }

    /**
     * 添加社团信息
     *
     * @param society 社团实体类
     * @return returnData
     */
    @PostMapping("/addSociety")
    @ResponseBody
    public ReturnData addSociety(@RequestBody Society society) {
        return societyService.addSociety(society);
    }

    /**
     * 修改社团信息
     *
     * @param society 社团实体类
     * @return returnData
     */
    @PostMapping("/updateSociety")
    @ResponseBody
    public ReturnData updateSociety(@RequestBody Society society) {
        return societyService.updateSociety(society);
    }

    /**
     * 删除社团信息
     *
     * @param societyId 社团id
     * @return returnData
     */
    @PostMapping("/deleteSociety")
    @ResponseBody
    public ReturnData deleteSociety(Integer societyId) {
        return societyService.deleteSociety(societyId);
    }
}
